#通过datetime模块完成时间戳，datetime.datetime对象，格式化字符串三者的转换
#from _datetime import datetime
import datetime
from datetime import timedelta
import time

#获取指定时区的时间,这个now实际上是datetime.datetime对象，>>>datetime.datetime(2021, 1, 5, 22, 42, 10, 284946)
now = datetime.datetime.now(tz=None)
print("当前时区时间：",now)

#格式化输出时间,datetime.datetime对象转化为字符串，>>> '2021-01-05 22:42:10'
now = now.strftime("%Y-%m-%d %H:%M:%S")
print("datetime.datetime->str:",now)
now_2 = now

#字符串转化为datetime.datetime对象，>>> datetime.datetime(2021, 1, 5, 22, 42, 10)
now = datetime.datetime.strptime(now,"%Y-%m-%d %H:%M:%S")
print("str->datetime.datetime:",now)

#datetime.datetime对象转化为时间戳timestamp,>>> 1609857730.0
now = now.timestamp()
print("datetime.datetime->timestamp:",now)
now_1 = now

#时间戳timestamp转化为datetime.datetime对象,>>> datetime.datetime(2021, 1, 5, 22, 42, 10)
now = datetime.datetime.fromtimestamp(now,tz=None)
print("timestamp->datetime.datetime:",now)

#时间戳转化为字符串(时间戳->datetime.datetime->字符串)
now_1 = datetime.datetime.fromtimestamp(now_1,tz=None)
now_1 = now_1.strftime("%Y-%m-%d %H:%M:%S")
print("timestamp->str:",now_1)

#字符串转化为时间戳(str->datetime.datetime->timestamp)
now_2 = datetime.datetime.strptime(now_2,"%Y-%m-%d %H:%M:%S")
now_2 = now_2.timestamp()
print("str->timestamp:",now_2)

#封装一个函数get_date(day_delta),如果输入-1，返回当前日期减1的字符串

print("------------------------------------------------")
def get_date_deco(func):
    def wrapperr(*args,**kwargs):
        #这里的参数居然是传入get_date函数的参数day_delta
        #for i in args:
            #print(i)
        result = func(*args,**kwargs)
        print(f"当你传入参数{args}进行运算后的日期为：{result}")
        return result
    return wrapperr

@get_date_deco
def get_date(day_delta):
    day = datetime.datetime.now(tz=None)
    print(f"当前日期为：{day.strftime('%Y-%m-%d')}")
    #需要先进行时间运算后，再格式化日期，顺序相反会报错
    day = day + timedelta(days=day_delta)
    day = day.strftime("%Y-%m-%d")
    return day

get_date(10)